<template>
  <van-loading color="#1989fa" v-if="!article.art_id">文章疯狂加载ing...</van-loading>
  <div class="article-detail__container" v-else>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ article.title }}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="article.aut_name" :label="$timeAgo(article.pubdate)">
        <template #icon>
          <img :src="article.aut_photo" alt="" class="avatar">
        </template>
        <template #default>
          <div>
            <van-button type="info" size="mini" v-if="article.is_followed" @click="handleFollowOrUnfollow(false)">已关注</van-button>
            <van-button icon="plus" type="info" size="mini" plain v-else @click="handleFollowOrUnfollow(true)">关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider>start</van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="article.content" v-highlight></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button icon="good-job" type="danger" size="small" v-if="article.attitude === 1" @click="handleLikeOrDislike(false)">已点赞</van-button>
        <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="handleLikeOrDislike(true)">点赞</van-button>
      </div>
      <!-- 文章评论 -->
      <comment-list />
    </div>
  </div>
</template>

<script>
import { getArticleDetail, follow, unFollow, articleLike, articleDislike } from '@/api/article'
import CommentList from './comment.vue'
export default {
  name: 'ArticleDetail',

  components: { CommentList },

  data () {
    return {
      article: {}
    }
  },

  // 组件缓存的目的(提高性能)：1.保持组件的状态。2.减少无效网络的请求
  // 注意：1.一旦组件被缓存了，created 钩子只会在组件初始化时执行一次，destroyed()钩子一次都不会执行
  // 2.要想知道这时候组件是被激活了还是失活了，需要借助2个专门配合组件缓存的钩子：activited(监测组件激活) 和 deactivated(监测组件失活)
  // 3.可以给keep-alive组件添加 include 或 exclude属性，用来匹配哪些组件需要缓存，那些不需要缓存。
  // 写在include 或 exclude 中的字符串是组件的 name 属性，需要给对应的组件添加 name 属性

  // activated 钩子让组件每次被激活时都会触发一次
  // activated () {
  //   this.theArticleDetail()
  // },

  created () {
    this.theArticleDetail()
  },

  methods: {

    // 获取文章详情
    async theArticleDetail () {
      // 调接口发请求
      const resp = await getArticleDetail(this.$route.query.aid)
      // 保存文章详情
      this.article = resp.data
    },

    // 关注或取关
    async handleFollowOrUnfollow (flag) {
      // flag 为true表示关注，否则是取关
      try {
        flag ? await follow(this.article.aut_id) : await unFollow(this.article.aut_id)
        // 重新获取详细数据
        await this.theArticleDetail()
        // 成功的提示
        flag ? this.$toast.success('关注成功') : this.$toast.success('已取关')
      } catch {
        // 失败的提示
        flag ? this.$toast.fail('取关失败') : this.$toast.fail('关注失败')
      }
    },

    // 点赞与取消赞
    async handleLikeOrDislike (flag) {
      // flag 为true表示点赞，否则是取消赞
      // 调接口发请求
      try {
        flag ? await articleLike(this.article.art_id) : await articleDislike(this.article.art_id)
        // 获取文章详情
        await this.theArticleDetail()
        // 成功的提示
        flag ? this.$toast.success('点赞成功') : this.$toast.success('已取消赞')
      } catch {
        flag ? await articleLike(this.article.art_id) : await articleDislike(this.article.art_id)
        // 失败的提示
        flag ? this.$toast.fail('点赞失败') : this.$toast.fail('取消赞失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
    /deep/ img{
    width: 100%;
  }
    /deep/ pre {
        white-space: pre-wrap;
        word-wrap: break-word;
    }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
